<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@include file="/common/taglibs.jsp" %>
<%@include file="/common/javascript.jsp" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>QSO</title>
<script type="text/javascript">
	var dialog;
	var sendDialog;
	var maingrid;
	//加载正在处理中的问题
	function loadQuestion(options){
		var o= options || {url:"${ctx}/qso/instruct/questionList" };
		maingrid.setOptions(o);
		maingrid.loadData(false);//设置false,否则会再次发起请求
	}
	
	//问题选择事件
	function vmSelect() {

		$("#vmSelect").empty();
		var rows=maingrid.getCheckedRows();
		var row;
		var array=new Array();
		for(var i=0;i<rows.length;i++){
			row=rows[i];
			if(containsArray(array,row.stuId)){
				continue;
			}
			array.push(row.stuId);
			$("#vmSelect").append("<option value="+row.userId+">"+row.stuName+"</option>");
		 }
		
		
		if(sendDialog){
			if(rows.length>0){
				sendDialog.setContent("确定发送资料吗!");
			}else{
				sendDialog.setContent("请选择要发送的学生!");
			}
		}
	}

	//点击 状态过滤
	function filterQuestion(checkObj) {
		$("#questionToolBar").find("input[name='filterQuestion']").each(
			function(index) {
				$(this).attr("checked", false);
		});
		
		if(checkObj.value=="WT"){
			$("#menuBar").show();
		}else{
			$("#menuBar").hide();
		}
		checkObj.checked=true;
		var options = {
			parms : [ {
				name : 'search_EQ_hpqStatus',
				value : checkObj.value
			} ],
			url : "${ctx}/qso/instruct/questionList"
		};
		loadQuestion(options);

	}

	//获得所有选中的用户id
	function getCheckedUserIds(){
		var rows=maingrid.getCheckedRows();
		var row;
		var array=new Array();
		for(var i=0;i<rows.length;i++){
			row=rows[i];
			if(containsArray(array,row.userId)){
				continue;
			}
			array.push(row.userId);
		 }
		return array.join();
	}
	
	//获得所有选中的问题id
	function getCheckedHpqIds(){
		var rows=maingrid.getCheckedRows();
		var row;
		var array=new Array();
		for(var i=0;i<rows.length;i++){
			row=rows[i];
			if(containsArray(array,row.hpqId)){
				continue;
			}
			array.push(row.hpqId);
		 }
		return array.join();
	}
	
    

	function toInstruct(groupId){
		var parent=window.parent;
		var geo=getWinGeometry(parent.window);
		var checkedHpqIds=getCheckedHpqIds();
		dialog=parent.$.ligerDialog.open({url: '${ctx}/qso/instruct/toInstruct/'+groupId,height:geo.height-40,width:geo.width,
		name:"instruct",isHidden:false,showMax: true, showToggle: true, showMin: true,isDrag:false,isResize: true, modal: true, title:"指导",onBeforeClose:
			function(){
				$(parent.frames["instruct"].document).find("#ViewerX").hide();
				parent.$.ligerDialog.confirm('关闭将自动完成当前问题,确定吗?', function(flag) {
					//执行完成操作
					if(flag){
						finishQuestion(groupId,checkedHpqIds);
					}
					else {
						$(parent.frames["instruct"].document).find("#ViewerX").show();
					}
				});
			
			},buttons:[{text:"完成",onclick:function(item,d){
				$(parent.frames["instruct"].document).find("#ViewerX").hide();
				parent.$.ligerDialog.confirm('确定完成当前问题吗?', function(flag) {
					//执行完成操作
					if(flag){
						finishQuestion(groupId,checkedHpqIds);
					}
					else {
						$(parent.frames["instruct"].document).find("#ViewerX").show();
					}
						
				});
			}}]});
			window.parent.dialogInstruct.close();
	} 
	
	//指导
	function openDiscussion(){
		var topic=$("#topic").val();
		var stuIds=getCheckedUserIds();
		var checkedHpqIds=getCheckedHpqIds();
		if(stuIds.length==0){
			LG.tip("请选择指导的学生!");
			return;
		}
		if($.trim(topic)==""){
			LG.tip("请输入主题!");
			return;
		}
		//要连接vm的学生(对应的用户id)
		var vmUserId=$("#vmSelect").val(); 
		var vmUserName=$("#vmSelect").text();
		//首先开启一个新的讨论组,然后跳转到指导页面(聊天)
		
 		LG.ajax({
			url:"${ctx}/qso/instruct/openDiscussion",
			data:{vmUserId:vmUserId,vmUserName:vmUserName,stuIds:stuIds,topic:topic,hpqIds:checkedHpqIds},
			success:function(result){
				if(!result.success){
					alert(result.message);
				}else{
					toInstruct(result.message);
				}
				
			},error:function(a){
				alert("error:"+a);
			}
		}); 
		
	}

	//完成讨论
	function finishQuestion(groupId,hpqIds){
		LG.ajax({
			url : "${ctx}/qso/instruct/finishQuestion",
			loading : '正在操作中...',
			data : {
				groupId:groupId,
				ids :hpqIds
			},
			success : function(result) {
				dialog.close();
				loadQuestion();
				
			},
			error : function(result) {
				LG.showError(result.message);
			}
		});
	}
	
	//拒绝
	function refuseQuestion() {
		var data = maingrid.getCheckedRows();
		if (data.length == 0)
			LG.tip('请选择行!');
		else {
			var checkedIdArray = [];
			var userIdArray=[];
			$(data).each(function() {
				checkedIdArray.push(this.hpqId);
				userIdArray.push(this.userId);
			});
			$.ligerDialog.confirm('确定拒绝 吗?', function(flag) {
				if (flag) {
					LG.ajax({
						url : "${ctx}/qso/instruct/refuseQuestion",
						loading : '正在操作中...',
						data : {
							hpqIds : checkedIdArray.join(','),
							userIds:userIdArray.join(',')
						},
						success : function(result) {
							LG.showSuccess(result.message);
							loadQuestion();
						},
						error : function(result) {
							LG.showError(result.message);
						}
					});
				}

			});
		}
	}
	
	function sendReferences(groupId,userIds){
		
		var hpqIds=getCheckedHpqIds();
    	LG.ajax({
    		url:"${ctx}/qso/instruct/sendReferences",
    		data:{refGroupId:groupId,sendIds:userIds,hpqIds:hpqIds},
    		success:function(result){
    			if(result.success){
    				 $.ligerDialog.success("发送成功！");
						loadQuestion();
    			}else{
    				$.ligerDialog.error("发送失败！");
    			}
    		}
    	});
	}
	
	function toHistoryList(){
		var geo=getWinGeometry(window);
		dialog=$.ligerDialog.open({url: '${ctx}/qso/instruct/toHistoryList/',name:"historyDialog",isHidden:false,height:geo.height-40,width:geo.width-20,showMax:true,showToggle:true,showMin:true,isResize:false,isDrag:false,modal:true,title:"历史问题",
			buttons: [ 
			           { text: '提交', onclick: function (item, dialog) {
			        	   var frame=window.frames["historyDialog"];
			        	   if(!frame){
			        		   $.ligerDialog.error("没有获得子窗口句柄！");
			        		   return;
			        	   }
			        	   var selectedId=frame.getSelectGroupId();
			        	   	if($.trim(selectedId)!=""){
			        	   		dialog.close();
			        			var stuIds=getCheckedUserIds();
			        			var sendDialogContent;
			        			if(stuIds.length==0){
			        				sendDialogContent="请选择要发送的学生!";
			        			}else{
			        				sendDialogContent="确定发送资料吗!";
			        			}
		        				sendDialog=$.ligerDialog.open({name:"sendDialog",type:"warn",modal:false,content:sendDialogContent,buttons:[{
		        					text:"发送",onclick:function(item,confirm){
		        						stuIds=getCheckedUserIds();
		        						if(stuIds.length==0){
		        							return;
		        						}else{
		        							confirm.close();
		        							sendReferences(selectedId,stuIds);
		        							//alert("发送数据啊！");
		        						}
		        				}},{text:'取消',onclick:function(item,confirm){
		        					confirm.close();
		        					dialog.close();
		        				}
		        				}]}); 
			        	   	}else{
			        	   		alert("请选择行!");
			        	   	}

							
						}}, { text: '返回',onclick: function (item, dialog) {
							dialog.close(); 
						} } 
			]
		});
		
	}

	$(function() {
		
		//预加载dialog的背景图片
		LG.prevDialogImage("${ctx}");

		$("#pageloading").hide();
        
		maingrid = $("#maingrid").ligerGrid({
			columns : [
				{display : '姓名',name : 'stuName',width : 80,minWidth : 60}, 
			    {display : '问题',name : 'hpqQuestion',width : 320,minWidth : 60}, 
				{display : '时间',name : 'dateCre',width : 150,minWidth : 60}, 
				{display : '状态',name : 'hpqStatus',width : 150,minWidth : 60},
				{name : 'userId',hide: true,width:1},
		        {
		            display: '操作', isAllowHide: false,width:150,minWidth:60,
		            render: function (row)
		            {	
		            	if(row.status == 'DC'){
		            		return '<a href="javascript:void(0);" onclick="toInstruct(\'' + row.groupId + '\')">继续讨论</a>';
		            	}
		                return "";
		            }
		        }
				//,{name:'hpqId',hide:true}
			],dataAction: 'server', url: '${ctx}/qso/instruct/questionList',pkName : 'hpqId',
			 width : '99%',height : '93%',checkbox : true,usePager : false,
			 parms : [{name : 'search_EQ_hpqStatus',value : 'WT'}],
             onCheckRow:vmSelect,
             onCheckAllRow:vmSelect
		});

		$(".l-toolbar-item").hover(function() {
			$(this).addClass("l-panel-btn-over");
		}, function() {
			$(this).removeClass("l-panel-btn-over");
		});

	});
</script>

</head>
<body style="padding:0px;">
	<div id="pageloading" style="display: block;"></div>
    <div style=" width:100%" id="questionToolBar">
    <div class="searchbox" style="padding:10px;">
        	<div style="float:left;color:#444;">状态:</div>
        	<div style="float:left; margin-left:15px;">
        		<input type="checkbox" name="filterQuestion" onclick="filterQuestion(this)" value="WT" checked="checked" style="vertical-align:middle;"/>
        		<span>等待</span>
        	</div>
        	<div style="float:left; margin-left:10px;">
        		<input type="checkbox" name="filterQuestion" onclick="filterQuestion(this)" value="CO" style="vertical-align:middle;"/> 
        		<span>完成</span>
        	</div>
        	<div style="float:left; margin-left:10px;">
        		<input type="checkbox" name="filterQuestion" onclick="filterQuestion(this)" value="DL" style="vertical-align:middle;"/> 
        		<span>拒绝</span>
        	</div>
        	<div style="float:left; margin-left:10px;">
        		<input type="checkbox" name="filterQuestion" onclick="filterQuestion(this)" value="DC" style="vertical-align:middle;"/> 
        		<span>讨论</span>
        	</div>
        	<div class="l-clear"></div>
    </div>
	</div>
    	 
	<div id="maingrid" style="margin: 0; padding: 0"></div>
	<div id="menuBar">
			<div style="float:left; margin-left:10px;margin-top:5px;">
			<label style="color:#444;">讨论主题<font style="color: red;">*</font></label>
			<input type="text" id="topic"/>
			<label style="color:#444;">虚机地址</label>
			<select id="vmSelect">
			</select>
			</div>
			<div class="inst-chat-send" style="float:left;margin-left:10px;">
            	<div style="width:60px" class="button button2" onclick="openDiscussion()">
					<div class="button-l"> </div>
					<div class="button-r"> </div> 
					<div class="button-icon"><img src="${ctx}/static/icons/main/inst-icon-01.png" style="vertical-align:middle;" /> </div>
					<span>指导</span>
				</div>
            </div>
           	<div class="inst-chat-send" style="float:left;margin-left:10px;">
            	<div style="width:60px" class="button button2" onclick="refuseQuestion()">
					<div class="button-l"> </div>
					<div class="button-r"> </div> 
					<div class="button-icon"><img src="${ctx}/static/icons/main/inst-icon-02.png" style="vertical-align:middle;" /> </div>
					<span>拒绝</span>
				</div>
            </div> 
            <div class="inst-chat-send" style="float:left;margin-left:10px;">
            	<div style="width:90px" class="button button2" onclick="toHistoryList()">
					<div class="button-l"> </div>
					<div class="button-r"> </div> 
					<div class="button-icon"><img src="${ctx}/static/icons/main/inst-icon-03.png" style="vertical-align:middle;" /> </div>
					<span>历史问题</span>
				</div>
            </div>  
	</div>
	<div style="display: none;"></div>
</body>
</html>